package utils.table;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.googlecode.objectify.Query;



import play.mvc.Http.Request;

public class DataTable<T> extends HashMap<String, Object> {
	private static final String ROWS_PROP = "rows";
	private static final String START_PROP = "start";
	private static final String DISPALY_LENGTH_PROP = "iDisplayLength";
	private static final String TOTAL_DISPLAY_PROP = "iTotalDisplayRecords";
	private static final String TOTAL_PROP = "iTotalRecords";
	
	private Query<T> query;
	
	public DataTable(Query<T> query, DataTablesParameters<T> parameters) {
		
		this.put(START_PROP, parameters.page);
		this.put(DISPALY_LENGTH_PROP, parameters.pageSize);
		this.put(TOTAL_DISPLAY_PROP, query.count());
		this.put(TOTAL_PROP, query.count());
		
		// Order
		if(parameters.order != null) query = query.order(parameters.order+parameters.orderBy);
		// Pagination
		int page = parameters.page > 0 ? parameters.page-1 : 0;
		query = query.limit(parameters.pageSize).offset(page*parameters.pageSize);
		//for(int i = 1; i < parameters.page ; i++) query = query.nextPage();
		
		this.put(ROWS_PROP, query.list());
	}
	
	public DataTable(List<T> list, DataTablesParameters<T> parameters) {
		this.put(START_PROP, parameters.page);
		this.put(DISPALY_LENGTH_PROP, parameters.pageSize);
		this.put(TOTAL_DISPLAY_PROP, list.size());
		this.put(TOTAL_PROP, list.size());
		
		int page = parameters.page > 0 ? parameters.page-1 : 0;
		this.put(ROWS_PROP, list.subList(page*parameters.pageSize, (page*parameters.pageSize)+parameters.pageSize));
	}
	
	public List<T> getRows() {
		return (List<T>) this.get(ROWS_PROP);
	}
	
	public void setRows(List rows) {
		this.put(ROWS_PROP, rows);
	}
	
	public static class DataTablesParameters<T> {
		  public int page;
		  public Integer pageSize;
		  public String search;
		  public String orderBy;
		  public String order;
		 
		  public DataTablesParameters(final Request request) {
		    		 
		     // Paging
		     final Integer startRow = request.params.get("iDisplayStart", Integer.class);
		     this.pageSize = request.params.get("iDisplayLength", Integer.class);
		     this.page = startRow == null ? 1 : (startRow / this.pageSize) + 1;
		 
		     // Sorting (first sort column only).
		     	final Integer sortingColumns = request.params.get("iSortingCols", Integer.class);
		     if (sortingColumns != null) {
		    	 final Integer columns = request.params.get("iColumns", Integer.class);
			     String[] cols = new String[columns];
			     for(int i = 0; i < columns ; i++) cols[i] = request.params.get("mDataProp_"+i);
			     final int sortColumnIndex = request.params.get("iSortCol_0", Integer.class);
		        this.orderBy = cols[sortColumnIndex]; //TODO from my test
		  
		        final String sortDirection = request.params.get("sSortDir_0");
		        if(sortDirection == null) {
		        	this.order = null;
		        } else {
		        	if("desc".toUpperCase().equals(sortDirection.toUpperCase())) this.order = "";
		        	else this.order = "-";
		        }
		        
		     }
		      
		     // Search
		     this.search = request.params.get("sSearch");
		  }
		}
}
